import modelExtend from '../../../utils/modelExtend';
import HttpModel from '../../../models/http';
import {
    doPostToObj, doPostParam
} from '@/services/http'
let fieldSetting = {
    namespace: 'fieldSetting',
    state: {
        fieldSettingList: [],
        loading: true,
        groupList: [],
        groupCount: 0, //   分组个数
        fieldCount: 0, //   字段个数
        modalLoading: true,
    },
    effects: {
        *settingCount({ payload, callback }, { put, call }) {
            yield put({
                type: "save",
                payload: { loading: true },
            });
            const response = yield call(doPostToObj, payload);
            yield put({
                type: "save",
                payload:
                    response.statusCode !== "0"
                        ? { loading: false }
                        : {
                            groupCount: response.data?.groupCount,
                            fieldCount: response.data?.fieldCount,
                            loading: false,
                        },
            });
            if (callback) callback(response);
            return response;
        },
        *groupList({ payload, callback }, { put, call }) {
            yield put({
                type: "save",
                payload: { modalLoading: true },
            });
            const response = yield call(doPostToObj, payload);
            yield put({
                type: "save",
                payload: { modalLoading: false, groupList: response.data },
            });
            if (callback) callback(response);
            return response;
        },
        *groupSave({ payload, callback }, { put, call }) {
            yield put({
                type: "save",
                payload: { modalLoading: true },
            });
            const response = yield call(doPostToObj, payload);
            yield put({
                type: "save",
                payload: { modalLoading: false },
            });
            if (callback) callback(response);
            return response;
        },
        *fieldSettingList({ payload, callback }, { put, call }) {
            yield put({
                type: "save",
                payload: { loading: true },
            });
            const response = yield call(doPostParam, payload);
            yield put({
                type: "save",
                payload: { loading: false, fieldSettingList: response.data },
            });
            if (callback) callback(response);
            return response;
        },
        *fieldSettingDetail({ payload, callback }, { put, call }) {
            yield put({
                type: "save",
                payload: { loading: true },
            });
            const response = yield call(doPostParam, payload);
            yield put({
                type: "save",
                payload: { loading: false },
            });
            if (callback) callback(response);
            return response;
        },
        *fieldSettingDel({ payload, callback }, { put, call }) {
            yield put({
                type: "save",
                payload: { loading: true },
            });
            const response = yield call(doPostParam, payload);
            yield put({
                type: "save",
                payload: { loading: false },
            });
            if (callback) callback(response);
            return response;
        },
        *fieldSettingStatus({ payload, callback }, { put, call }) {
            yield put({
                type: "save",
                payload: { loading: true },
            });
            const response = yield call(doPostParam, payload);
            yield put({
                type: "save",
                payload: { loading: false },
            });
            if (callback) callback(response);
            return response;
        },
        *fieldSettingSave({ payload, callback }, { put, call }) {
            yield put({
                type: "save",
                payload: { modalLoading: true },
            });
            const response = yield call(doPostToObj, payload);
            yield put({
                type: "save",
                payload: { modalLoading: false },
            });
            if (callback) callback(response);
            return response;
        },
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
};
fieldSetting = modelExtend(HttpModel, fieldSetting);
export default fieldSetting;